package br.com.naybee.client.views.add.configuracao;

import java.util.ArrayList;
import java.util.List;

import javax.management.Notification;

import br.com.naybee.client.NaybeeService;
import br.com.naybee.client.views.add.menu.configuracao.MenuConfiguracaoView;
import br.com.naybee.client.views.base.Principal;
import br.com.naybee.framework.client.componente.gwt.event.CommEvent;
import br.com.naybee.framework.client.componente.gwt.event.CommHandler;
import br.com.naybee.framework.client.componente.gwt.web.Div;
import br.com.naybee.framework.client.models.ListModel;
import br.com.naybee.framework.client.models.Model;
import br.com.naybee.framework.client.models.Models;
import br.com.naybee.framework.client.notification.NotificationSimple;
import br.com.naybee.framework.client.utils.NAYException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class CadastrarIconesView extends Composite {

	private static CadastrarIconesUiBinder uiBinder = GWT.create(CadastrarIconesUiBinder.class);

	interface CadastrarIconesUiBinder extends UiBinder<Widget, CadastrarIconesView> {
	}

	private List<String> ListaIcones = new ArrayList<String>();

	@UiField
	protected Div automacao, banheiro, configuracao, cozinha2, cozinha, garagem, grupoRepartimento, icones, lavanderia, menu, porta, quartoCasal,
			quartoSolteiro, repartimento, salaEstar, salaJantar, salaJantar1, iluminacao;

	public CadastrarIconesView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiHandler("automacao")
	public void onClickautomacao(ClickEvent event) {
		selection(automacao);
	}

	@UiHandler("banheiro")
	public void onClickbanheiro(ClickEvent event) {
		selection(banheiro);
	}

	@UiHandler("configuracao")
	public void onClickconfiguracao(ClickEvent event) {
		selection(configuracao);
	}

	@UiHandler("cozinha2")
	public void onClickcozinha2(ClickEvent event) {
		selection(cozinha2);
	}

	@UiHandler("cozinha")
	public void onClickcozinha(ClickEvent event) {
		selection(cozinha);
	}

	@UiHandler("garagem")
	public void onClickgaragem(ClickEvent event) {
		selection(garagem);
	}

	@UiHandler("grupoRepartimento")
	public void onClickgrupoRepartimento(ClickEvent event) {
		selection(grupoRepartimento);
	}

	@UiHandler("icones")
	public void onClickicones(ClickEvent event) {
		selection(icones);
	}

	@UiHandler("lavanderia")
	public void onClicklavanderia(ClickEvent event) {
		selection(lavanderia);
	}

	@UiHandler("menu")
	public void onClickmenu(ClickEvent event) {
		selection(menu);
	}

	@UiHandler("porta")
	public void onClickporta(ClickEvent event) {
		selection(porta);
	}

	@UiHandler("quartoCasal")
	public void onClickquartoCasal(ClickEvent event) {
		selection(quartoCasal);
	}

	@UiHandler("quartoSolteiro")
	public void onClickquartoSolteiro(ClickEvent event) {
		selection(quartoSolteiro);
	}

	@UiHandler("repartimento")
	public void onClickrepartimento(ClickEvent event) {
		selection(repartimento);
	}

	@UiHandler("salaEstar")
	public void onClicksalaEstar(ClickEvent event) {
		selection(salaEstar);
	}

	@UiHandler("salaJantar")
	public void onClicksalaJantar(ClickEvent event) {
		selection(salaJantar);
	}

	@UiHandler("salaJantar1")
	public void onClicksalaJantar1(ClickEvent event) {
		selection(salaJantar1);
	}

	@UiHandler("iluminacao")
	public void onClickiluminacao(ClickEvent event) {
		selection(iluminacao);
	}

	@UiHandler("btnSalvar")
	public void onClickBtnSalvar(ClickEvent event) {
		Model icones = Models.newModel().add("icones", carregarIcones());
		NaybeeService naybeeService = new NaybeeService("iconeFs", "registrarIcones");
		naybeeService.post(icones, new CommHandler() {
			public void onSuccess(CommEvent event) {
				Principal.get().add(new MenuConfiguracaoView());
			}

			public void onException(NAYException error) {
				NotificationSimple.showErrorToast("Ocorreu um erro no servico.");
			}
		});
	}

	private ListModel carregarIcones() {
		ListModel listModel = new ListModel();
		for (String icone : ListaIcones) {
			Model model = Models.newModel();
			model.add("icone", icone);
			listModel.add(model);
		}
		return listModel;
	}

	private void selection(Div div) {
		if (!div.getClassName().contains("selection")) {
			String className = div.getClassName() + " selection";
			div.setClassName("");
			div.setClassName(className);
			ListaIcones.add(div.getValue());
		} else {
			String className = div.getClassName().replaceAll("selection", "");
			div.setClassName("");
			div.setClassName(className);
			ListaIcones.remove(div.getValue());
		}
	}

}
